package controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.entity.SysUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 吴阳基
 * @datetime 2024/1/16 16:05
 */
@Controller
@RequestMapping("/hello")//请求映射，对应的是请求路径，如果是多个请求路径，可以使用@RequestMapping�
public class HelloController extends AbstractController {
    private Logger logger = Logger.getLogger(HelloController.class);

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("Spring MVC 框架搭建成功 ");
        return new ModelAndView("hello");
    }

    @RequestMapping("/hello")//请求映射，对应的是请求路径，如果是多个请求路径，可以使用@RequestMapping注
    public ModelAndView hello(@RequestParam(value = "userName", required = false) String realName) {
        logger.info("你好" + realName + "，欢迎来到 sprig MVC  课堂！ ");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName", realName);
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/hello2")//请求映射，对应的是请求路径，如果是多个请求路径，可以使用@RequestMapping注
    public String hello2(Model model, @RequestParam String realName) {
        logger.info("欢迎" + realName + "来到spring MVC 课堂！");
        model.addAttribute("realName", realName);
        model.addAttribute(realName);

        SysUser sysUser = new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser", sysUser);
        model.addAttribute(sysUser);
        return "hello";
    }

    @RequestMapping("/hello3")//请求映射，对应的是请求路径，如果是多个请求路径，可以使用@RequestMapping�

    public String hello3(Map<String, Object> map, Model model,
                         @RequestParam String realName) {
        logger.info("欢迎" + realName + "来到spring MVC 课堂！");
        model.addAttribute("realName", realName);
        map.put("realName", realName);
        return "hello";
    }

}
